<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
    <title>Hello WebSocket</title>
    <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="main.css" rel="stylesheet">
    <script src="/webjars/jquery/jquery.min.js"></script>
    <script src="/webjars/sockjs-client/sockjs.min.js"></script>
    <script src="/webjars/stomp-websocket/stomp.min.js"></script>
    
    <script>
	    var stompClient = null;
	
	    function setConnected(connected) {
	        $("#connect").prop("disabled", connected);
	        $("#disconnect").prop("disabled", !connected);
	        if (connected) {
	            $("#conversation").show();
	        }
	        else {
	            $("#conversation").hide();
	        }
	        $("#notice").html("");
	    }
	
	    function connect() {
	        var socket = new SockJS('/endpoint-websocket/'); //连接上端点(基站)
	        console.dir(socket);
	        stompClient = Stomp.over(socket);			//用stom进行包装，规范协议
	        stompClient.connect({}, function (frame) {	
	            setConnected(true);
	            console.log('Connected: ' + frame);
	            stompClient.subscribe('/topic/game_chat', function (result) {
	            	console.info(result)
	            	showContent(JSON.parse(result.body));
	            });
	        });
	    }
	    

	    function connectWS(){
	    	var socket = new SockJS("/endpoint-websocket");
	    	stompClient = Stomp.over(socket);
	    	stompClient.connect({} , function(frame){
	    		setConnected(true);
	    		console.log("Connected: " + frame);
	    		stompClient.subscribe("/topic/game_chat",function(result){
	    			console.info(result);
	    			showContent(JSON.pares(result.body));
	    		});
	    	});
	    }
	
	    function disconnect() {
	        if (stompClient !== null) {
	            stompClient.disconnect();
	        }
	        setConnected(false);
	        console.log("Disconnected");
	    }
	
	    function sendName() {
	    	
	        stompClient.send("/app/v1/chat", {}, JSON.stringify({'content': $("#content").val()}));
	    }
	
	    function showContent(body) {
	        $("#notice").append("<tr><td>" + body.content + "</td> <td>"+new Date(body.time).toLocaleString()+"</td></tr>");
	    }
	
	
	
	
	
	    $(function () {
	        $("form").on('submit', function (e) {
	            e.preventDefault();
	        });
	        $( "#connect" ).click(function() { connectWS(); });
	        $( "#disconnect" ).click(function() { disconnect(); });
	        $( "#send" ).click(function() { sendName(); });
	    });

    </script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
    enabled. Please enable
    Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">
    <div class="row">
        <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="connect">建立连接通道:</label>
                    <button id="connect" class="btn btn-default" type="submit">Connect</button>
                    <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
                    </button>
                </div>
            </form>
        </div>
        <!-- <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="name">发布新公告</label>
                    <input type="text" id="content" class="form-control" placeholder="请输入...">
                </div>
                <button id="send" class="btn btn-default" type="submit">发布</button>
            </form>
        </div> -->
    </div>
    <div class="row">
        <div class="col-md-12">
            <table id="conversation" class="table table-striped">
                <thead>
                <tr>
                    <th>游戏公告内容</th>
                </tr>
                </thead>
                <tbody id="notice">
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
